package com.zl;

import com.alibaba.fastjson.JSON;
import com.zl.entity.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.io.*;
import java.util.Date;

/**
 * jedis支持两种存储模式
 * 1.key 字符串 value 字符串
 * 2.key byte[]  value  byte[]
 *
 * 存储对象的原理就是把对象转换为byte[] 或者 json
 */
public class SetGetTest {
    private Jedis jedis;

    //before是Junit的注解，表示在执行测试代码前都需要执行该代码
    @Before
    public void connectToReids() {
        //1.连接redis
        //host port
        jedis = new Jedis("127.0.0.1", 6379);
        //密码
        jedis.auth("DEVPASSWORD");
        //选择库
        jedis.select(3);
    }

    @After
    public void closeJedis() {
        this.jedis.close();
    }

    /**
     * 存入string
     */
    @Test
    public void setString() {
        jedis.set("name", "刘德华");
        //3.释放资源
        jedis.close();
    }

    /**
     * 获取string
     */
    @Test
    public void getString() {
        System.out.println(jedis.get("name"));
    }

    /**
     * 以字节数组的形式存储对象
     * object to byte[]
     */
    @Test
    public void setbyteArrayObject() {
        User user = new User(1, "张三", new Date());
        String key = "user";

        byte[] keyarray = serialize(key);
        byte[] valueArray = serialize(user);

        jedis.set(keyarray, valueArray);
    }

    /**
     * 获取字节数组
     */
    @Test
    public void getByteArrayObject() {
        String key = "user";
        byte[] keyarray = serialize(key);
        byte[] valueArray = jedis.get(keyarray);
        User user = (User) deserialize(valueArray);
        System.out.println(user.getId());
        System.out.println(user.getName());
        System.out.println(user.getBirthday());
    }

    /**
     * object to json
     */
    @Test
    public void setStringObject() {
        String key = "stringUser";
        User user = new User(1, "lisi", new Date());
        String jsonString = JSON.toJSONString(user);
        jedis.set(key, jsonString);
    }

    @Test
    public void getStringObject() {
        String json = jedis.get("stringUser");
        User user = JSON.parseObject(json, User.class);
        System.out.println(user.getName());
    }


    /**
     * 对象转成字节数组
     * @param object
     * @return
     */
    public static byte[] serialize(Object object) {
        if (object == null) {
            return null;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        try {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            oos.flush();
        }
        catch (IOException ex) {
            throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);
        }
        return baos.toByteArray();
    }

    /**
     * 字节数组转为对象
     * @param bytes
     * @return
     */
    public static Object deserialize(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        try {
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
            return ois.readObject();
        }
        catch (IOException ex) {
            throw new IllegalArgumentException("Failed to deserialize object", ex);
        }
        catch (ClassNotFoundException ex) {
            throw new IllegalStateException("Failed to deserialize object type", ex);
        }
    }
}
